import { Body, Controller, Delete, Get, HttpException, HttpStatus, Param, Patch, Post, Query } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { EEnableStatus } from '@/shared/types/globalEnum'
import { CreateQrCodeDto, FindQrCodeDto, UpdateQrCodeDto } from './dto/qr-code.dto'
import { QrCodeService } from './qr-code.service'

@ApiTags('后台/二维码配置')
@Controller('admin/qr-code')
export class QrCodeController {
  constructor(private readonly qrCodeService: QrCodeService) {}

  /**
   * 二维码配置列表
   */
  @Get()
  list(@Query() query: FindQrCodeDto) {
    return this.qrCodeService.findList(query)
  }

  /**
   * 添加二维码配置
   */
  @Post()
  create(@Body() data: CreateQrCodeDto) {
    return this.qrCodeService.create(data)
  }

  /**
   * 更新二维码配置
   */
  @Patch(':qrCodeId')
  async update(@Param('qrCodeId') qrCodeId: number, @Body() data: UpdateQrCodeDto) {
    const findData = await this.qrCodeService.findOne({
      qrCodeId,
    })
    if (!findData) {
      throw new HttpException('二维码配置不存在', HttpStatus.NOT_FOUND)
    }

    await this.qrCodeService.update(qrCodeId, data)
  }

  /**
   * 删除二维码配置
   */
  @Delete(':qrCodeId')
  async remove(@Param('qrCodeId') qrCodeId: number) {
    const findData = await this.qrCodeService.findOne({
      qrCodeId,
    })
    if (!findData) {
      throw new HttpException('二维码配置不存在', HttpStatus.NOT_FOUND)
    }

    await this.qrCodeService.remove(qrCodeId)
  }
}

@ApiTags('官网/二维码配置')
@Controller('web/qr-code')
export class WebQrCodeController {
  constructor(private readonly qrCodeService: QrCodeService) {}

  /**
   * 启用的二维码配置列表
   */
  @Get()
  webList(@Query() query: FindQrCodeDto) {
    return this.qrCodeService.findList({
      enableStatus: EEnableStatus.ENABLED,
      ...query,
    })
  }
}
